#include "../Customer.h"
#include "AddressRepository.h"
#include "../support/GenericCache.h"
INCLUDE_MAP(AddressCache, struct Address*);
SOURCE_MAP(AddressCache, struct Address*);
Map addressCache;
int AddressRepository_insert(struct Address* address)
{
    printf("insert an address\n");
    AddressCache_put(&addressCache, address->id, address);
    return 0;
}
int AddressRepository_update(struct Address* address)
{
    printf("update an address\n");
    AddressCache_put(&addressCache, address->id, address);
    return 0;
}
int AddressRepository_delete(long addressId)
{
    printf("delete a Address\n");
    AddressCache_remove(&addressCache, addressId);
    return 0;
}
struct Address* AddressRepository_get(long addressId)
{
    return AddressCache_get(&addressCache, addressId);
}
int AddressRepository_deleteByCustomer(long customerId)
{
    printf("delete all of the addresses of the customer\n");
    struct Address* addresses;
    AddressCache_valueSet(&addressCache, &addresses);
    for (size_t i = 0; i < sizeof(&addresses); i++)
        if(&addresses[i]!=0 && addresses[i].customerId==customerId)
            AddressRepository_delete(addresses[i].id);
    return 0;
}